package com.hison.tandainbao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hison.tandainbao.entiy.pojo.domain.TbItems;
import com.hison.tandainbao.entiy.pojo.domain.User;
import com.hison.tandainbao.entiy.pojo.dto.ItemsAll;
import com.hison.tandainbao.entiy.pojo.dto.UserDTO;
import com.hison.tandainbao.mapper.UserMapper;
import com.hison.tandainbao.response.ResponseResult;
import com.hison.tandainbao.service.TbItemsService;
import com.hison.tandainbao.mapper.TbItemsMapper;
import com.hison.tandainbao.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author hison
 * @description 针对表【tb_items】的数据库操作Service实现
 * @createDate 2024-03-30 16:46:11
 */
@Service
public class TbItemsServiceImpl extends ServiceImpl<TbItemsMapper, TbItems>
        implements TbItemsService {

    @Autowired
    private TbItemsMapper mapper;

    @Override
    public ResponseResult getAll() {
        List<ItemsAll> list = mapper.getAll();
        if (list.size() < 0 ){
            return  ResponseResult.error("还没有相关信息");
        }
        return ResponseResult.ok(list);
    }

    @Override
    public ResponseResult addItem(TbItems tbItems) {
        //TODO 插入用户ID
        int i = mapper.insert(tbItems);
        if (i < 1) {
            return ResponseResult.error("插入失败");
        }
        return ResponseResult.ok("插入成功");
    }

    @Override
    public ResponseResult getMyPutItems() {


//        Long id = UserHolder.getUser().getId();
        QueryWrapper<TbItems> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", 1);
        List<TbItems> items = mapper.selectList(wrapper);
        if (items.size() <= 0) {
            return ResponseResult.error("您还未发布相关信息");
        }
        return ResponseResult.ok(items);
    }

    @Override
    public ResponseResult getItem(String name) {
        TbItems tbItems  = mapper.getItemByLikeName(name);
        if (tbItems == null){
            return ResponseResult.error("对不起,没找到");
        }
        return ResponseResult.ok(tbItems);
    }

    @Override
    public ResponseResult updateItem(Integer itemId,Integer type) {
        Long userId = UserHolder.getUser().getId();
        int i = mapper.updateItem(itemId,userId,type);
        if (i < 1 ){
            return  ResponseResult.error("修改失败");
        }
        return ResponseResult.ok("修改成功");
    }

    @Override
    public ResponseResult deleteItem(Integer itemId) {
        int i = mapper.deleteByItemId(itemId);
        if (i < 1){
            return  ResponseResult.error("删除失败");
        }
        return ResponseResult.ok("删除成功");
    }

    @Override
    public ResponseResult adminUpdateStatus(Integer itemId, Integer status) {
        int i = mapper.updateItemStatus(itemId, status);
        if (i< 1){
            return ResponseResult.error("审核失败");
        }
        return ResponseResult.ok("审核成功");
    }

    @Override
    public ResponseResult adminGetAll(Integer current, Integer total) {
        PageHelper.startPage(current,total);
        List<TbItems> list = query().orderByAsc("audit_status","type").list();
        list.forEach(item ->{
            //查询发布者的名字
            Long userId = item.getUserId();
            String userName = getUserName(userId);
            item.setUserName(userName);
        });
        if (list.size() <= 0 ){
            return  ResponseResult.error("查询失败");
        }
        return ResponseResult.ok(list);
    }

    @Autowired
    private UserMapper  userMapper;
    private String  getUserName(Long userId) {
        User user = userMapper.selectById(userId);
        return user.getName();
    }
}




